/*
* @file models/pdtPosition.js
*		产品管理/查看产品持仓
*/

import { routerRedux } from 'dva/router';
import api from '../api';
import moment from 'moment';

export default {
	namespace:'pdtposition',
	state:{
		poslist:[],
		currentpage:1,
		pageSize:15,
		filterDropdownVisible: false,
		name: '',//搜索股票代码
	  filtered: false,
	  loading:false
	},
	subscriptions:{
		setup({dispatch,history}) {
			history.listen(({pathname,query}) => {
				if( pathname === '/systemMgt/accountMgt/pdtMgt/examine'){
					dispatch({
						type: 'getList',
						payload: {
							...query,
						}
					})
				}
			})
		}
	},
	effects:{
	*	getList({payload:query},{call,put}){
			yield put({
				type:'initData',
				payload:{
					poslist:[],
					name:''
				}
			})
			const response = yield call(api.getPdtPositionList, query);
				yield put({
					type: 'getListSuccess',
					payload: {
						...response,
					},
				})
			
		},
		*changeRoute({payload:query},{call,put}){
			yield put(routerRedux.push({
				pathname:`/systemMgt/accountMgt/pdtMgt/examine`,
				query:{
					...query,
				}
			}))
		},
		*changeState({payload:query},{call,put}){
			yield put({
				type: 'getList',
				payload: {
					...query,
				}
			})
		},
		*alterState({payload:query},{call,put}){
			yield put({
				type:'initData',
				payload:{
					name:query.name,
					filterDropdownVisible:query.filterDropdownVisible,
					loading:query.loading
				}
			})
		}
	},
	reducers:{
		initData(state, {payload}) {
			return {
				...state,
				...payload
			}
		},
		getListSuccess(state, {payload}) {
			const {data,page} = payload
			data.forEach((item)=>{
				item.dateText = moment(item.date).format('YYYY/MM/DD')
			})
			const status=data ? true:false
			return {
				...state,
				status,
				poslist:data,
				page,
				loading:false
			}
		},
	}
}